# 迭代器
# l=iter(range(10))
# print(next(l))
# print(next(l))
# print(next(l))
# print(next(l))
# print(next(l))
# # 0
# # 1
# # 2
# # 3
# # 4
# 迭代器
#
# my_range=iter(range(10))
# print(my_range)
# print(next(my_range))
# print(next(my_range))
# print(next(my_range))
# <range_iterator object at 0x000001A789B25950>
# 0
# 1
# 2





# 生成器
#
# def my_gen(n):
#     for i in range(n):
#         yield i*i
#         print(f"current index:{i}")
#
# my_range=my_gen(10)
# print(my_range)
# print(next(my_range))
# print(next(my_range))
# print(next(my_range))
# # <generator object my_gen at 0x00000157670BE200>
# # 0
# # 1
# # 2
# # <generator object my_gen at 0x000002013558E200>
# # 0 暂停
#
# # current index:0
# # 1暂停
#
# # current index:1
# # 4 暂停

# # 协程
# def simple_coro(a):
#     print("初始值 a=",a)
#     b=yield a
#     print("初始值 b=",b)
#     c=yield a+b
#     print("初始值 c=",c)
#
# coro=simple_coro(1)
# print("调用next 方法,返回出去：",next(coro))#执行到b=yield a结束
# print(coro.send(2))#将2传给b，执行到 c=yield a+b结束
# print(coro.send(3))#将3传给c，执行到没有东西了；
# # 初始值 a= 1
# # 调用next 方法： 1
# # 初始值 b= None
# # 初始值 a= 1
# # 调用next 方法： 1
# # 初始值 b= 2
# # 3
# # 初始值 c= 3

# asynchronous[eɪˈsɪŋkrənəs]

# # 利用yield，计算平均数
# def coro_avg():
#     total=0
#     length=0
#     while True:
#         try:
#             value=yield total/length
#         except ZeroDivisionError:
#             value=yield 0
#
#         total+=value
#         length+=1
#
# my_avg=coro_avg()
# print(next(my_avg))#预激
# print(my_avg.send(3))#可以无限调用，计算平均数
# # 0
# # 3.0

# yield 和yield from返回最终值
def return_triple():
    while True:
        value=yield
        if value%3==0:
            return value
def triple_recoder():
    while True:
        result=yield  from  return_triple()
        triple_arr.append(result)

triple_arr=[]
coro=triple_recoder()
next(coro)
for i in range(10):
    coro.send(i)
print(triple_arr)
# [0, 3, 6, 9]



